import json
from django.http import HttpResponse
from django.core.cache import cache
from datetime import date, datetime,timedelta
import random
from booking.cache.caches import abtcache,getrange,check,U



def getmynums(request):
    data = json.loads(request.body)
    print(data)
    openid = data['openid']
    jihui = getrange(openid)
    synum = len(cache.get('quans'))
    if jihui:
        abtcache(openid)
        time = cache.get('ctime')
        return HttpResponse(json.dumps({"ret":"True"}))
    else:
        return HttpResponse(json.dumps({"ret":"False"}))

def cancel(request):
    data = json.loads(request.body)
    openid = data['openid']
    linenums = cache.get('linenums')
    if linenums:
        if openid in linenums:
            return HttpResponse(json.dumps({"ret":"F2"}))

    numsid = cache.get(openid)[0]
    quans = cache.get('quans')
    quans.append(numsid)
    cache.set('quans',quans,24*60*60)
    cache.set(openid,[],6)
    return HttpResponse(json.dumps({"ret":"True"}))


def checkbook(request):
    data = json.loads(request.body)
    openid = data['openid']
    check(datetime.now())
    jihui = cache.get(openid)
    worknums = len(cache.get('worknums'))
    linenums = len(cache.get('linenums'))
    quans = len(cache.get('quans'))
    rsp = {}
    rsp['worknums'] = worknums
    rsp['linenums'] = linenums
    rsp['quans'] = quans
    if jihui:
        rsp['time'] = cache.get('ctime')
        rsp['nums'] = cache.get('RJ')[jihui[0]] + U(jihui[0])
        rsp['st'] = jihui[1]
        rsp['code'] = jihui[0]
        rsp['stepNum'] = jihui[2]
        rsp['ret'] = "True"
        return HttpResponse(json.dumps(rsp))
    else:
        rsp['ret'] = 'False'
        return HttpResponse(json.dumps(rsp))


